package my.stone.asetdemo.ui.widget.bottomtab

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.google.android.material.bottomnavigation.BottomNavigationView
import kotlinx.coroutines.launch
import my.stone.asetdemo.R
import my.stone.asetdemo.common.bindView
import my.stone.asetdemo.common.dp
import my.stone.asetdemo.ui.widget.motion.MultiListenerMotionLayout
import kotlin.math.sqrt

class JHSBottomNavigationView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
): MultiListenerMotionLayout(context, attrs, defStyleAttr) {

    private val navigation: BottomNavigationView by bindView(R.id.navigation)
    private val addButton: ImageView by bindView(R.id.add)
    private val button1: View by bindView(R.id.b1)
    private val button2: View by bindView(R.id.b2)
    private val button3: View by bindView(R.id.b3)

    init {
        inflate(context, R.layout.activity_home_montion, this)
        enableClick()

        navigation.itemRippleColor = null
        navigation.isItemActiveIndicatorEnabled = false
    }

    fun enableClick() = when (currentState) {
        R.id.set_1 -> {
            addButton.setOnClickListener { setup() }
        }
        R.id.set_2 -> {
            addButton.setOnClickListener { close() }
            button1.setOnClickListener { Log.d("ST", "b1 ... ") }
            button2.setOnClickListener { Log.d("ST", "b2 ... ") }
            button3.setOnClickListener { Log.d("ST", "b3 ... ") }
            setOnClickListener { close() }
        }
        else -> {
        }
    }

    private fun disableClick() {
        addButton.setOnClickListener(null)
    }

    fun setup(): Unit = performAnimation {
        setTransition(R.id.set_1, R.id.set_2)
        transitionToState(R.id.set_2)
        // awaitTransitionComplete(R.id.set_2)
    }

    fun close(): Unit = performAnimation {
        setTransition(R.id.set_2, R.id.set_1)
        transitionToState(R.id.set_1)
        // awaitTransitionComplete(R.id.set_1)
    }

    private inline fun performAnimation(crossinline block: suspend () -> Unit) {
        (context as AppCompatActivity).lifecycleScope.launch {
            disableClick()
            block()
            enableClick()
        }
    }

}